package com.megvii.corelib.components;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.megvii.corelib.R;
import com.megvii.corelib.base.MegviiAdapter;

import java.util.List;

/**
 * Created by linyue on 16/2/3.
 */
public class IconTextAdapter extends MegviiAdapter<IconTextAdapter.ViewHolder>
{
    private List<Integer> iconRess;

    private List<String> texts;

    public IconTextAdapter(Context context)
    {
        super(context, ViewHolder.class, R.layout.icon_text_item);
    }

    public void updateData(List<Integer> iconRess, List<String> texts)
    {
        this.iconRess = iconRess;
        this.texts = texts;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if (convertView == null)
        {
            convertView = inflater.inflate(layoutId, null);
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.icon_text_layout_icon);
            holder.text = (TextView) convertView.findViewById(R.id.icon_text_layout_text);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        processViewHolder(holder, position);
        return convertView;
    }

    @Override
    protected void processViewHolder(ViewHolder viewHolder, int position)
    {
        viewHolder.icon.setImageResource(iconRess.get(position));
        viewHolder.text.setText(texts.get(position));
    }

    @Override
    public int getCount()
    {
        return texts == null ? 0 : texts.size();
    }

    @Override
    public Object getItem(int position)
    {
        return position;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    public static class ViewHolder
    {
        public ImageView icon;

        public TextView text;
    }
}
